import pandas as pd
import os,json,shutil




input_cd = 'D:\\00_python_auto\\douyin-downloader-main\\douyin-downloader-main\\download'   
output_cd = 'D:\\00_python_auto\\video2mp32txt\\00-orivideo'
pd_result = pd.DataFrame(columns=['likes','create_time','video_name',])
index_num = 0
for input_cd_i in os.listdir(input_cd):
    for cd_i in os.listdir(os.path.join(input_cd,input_cd_i,'post')):
        index_num += 1
        for cd2_i in  os.listdir(os.path.join(input_cd,input_cd_i,'post',cd_i)):
            if cd2_i.endswith('.json'):
                with open(os.path.join(input_cd,input_cd_i,'post',cd_i,cd2_i), 'r', encoding='utf-8') as file:
                    data = json.load(file)
                    pd_result.loc[index_num] = [data['statistics']['digg_count'],data['create_time'],data['desc'].replace('\\','').replace('\"','')]

        for cd2_i in  os.listdir(os.path.join(input_cd,input_cd_i,'post',cd_i)):
            if cd2_i.endswith('.mp4'):
                try:
                    shutil.copy2(os.path.join(input_cd,input_cd_i,'post',cd_i,cd2_i), output_cd+'/%s-%s.mp4'%(pd_result.loc[index_num,'likes'],pd_result.loc[index_num,'video_name'] ))
                except:
                    print(pd_result.loc[index_num,'video_name'])
                    input(cd2_i,'。。。。。。。。出错了！！！！！！！！！')

            

pd_result.sort_values(by='likes',ascending=False).to_csv(output_cd+'/allfilelist.csv',encoding='utf_8_sig',index=False)

